PHP 性能追踪及分析工具 xhprof 的安装与使用 | 您所在的位置:网站首页 › php 性能调试工具 › PHP 性能追踪及分析工具 xhprof 的安装与使用 |
对于本地开发环境来说,进行性能分析xdebug是够用了,但如果是线上环境的话,xdebug消耗较大,配置也不够灵活,因此线上环境建议使用xhprof进行PHP性能追踪及分析。 我们今天就简单介绍一下xhprof的简单安装与使用 xhprof的安装下载xhprof,我们这里选择的是通过git clone的方式,当然你也可以从 http://pecl.php.net/package/xhprof 这里下载。 cd /usr/local/src git clone https://github.com/phacility/xhprof.git注意: php5.4及以上版本不能在pecl中下载,不支持。需要在github上下载hhttps://github.com/phacility/xhprof.git。 另外xhprof已经很久没有更新过了,截至目前还不支持php7,php7可以试使用https://github.com/tideways/php-profiler-extension。 安装xhporof cd xhprof/extension /usr/local/php5.6/bin/phpize ./configure --with-php-config=/usr/local/php5.6/bin/php-config --enable-xhprof make make install最后如果出现类似的提示信息,就表示编译安装成功 stalling shared extensions: /usr/local/php-5.6.14/lib/php/extensions/no-debug-non-zts-20131226/修改配置文件/etc/php5.6.ini,在最后增加如下配置 [xhprof] extension=xhprof.so xhprof.output_dir=/data/www/xhprof/output重启php-fpm后通过phpinfo查看,或者在命令行通过php -m | grep xhprof查看是否安装成功。 注意: 需要创建output_dir mkdir -p /data/www/xhprof/output xhprof的简单用法将下载的xhprof复制到webroot目录,我这里以/data/www/project-xhprof为例 mkdir /data/www/project-xhprof cp -R /usr/local/src/xhprof/* /data/www/project-xhprof/ cd /data/www/project-xhprof在nginx中增加站点配置 server { listen 80; server_name xhprof.dev; root /data/www/project-xhprof; index index.php index.html; access_log /var/log/nginx/xhprof.dev.log main; error_log /var/log/nginx/xhprof.dev.log.err debug; rewrite_log on; location ~* \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/var/run/php5.6-fpm.sock; fastcgi_index index.php; } }重启nginx,然后使用http://xhprof.dev/examples/sample.php,可以看到一些输出,并且提示通过访问http:///index.php?run=XXX&source=xhprof_foo查看结果。接下来访问 http://xhprof.dev/xhprof_html/ 就可以看到已经保存的结果,列出了所有函数的调用以及所消耗的时间,如图: 分析一下示例代码sample.php,关健代码: |
CopyRight 2018-2019 实验室设备网 版权所有 |